package com.mgg.message.controller;

import java.util.HashMap;
import java.util.Map;

import com.mgg.common.enums.MessageNotifyEnum;
import com.mgg.common.enums.SmsTypeEnum;
import com.mgg.common.enums.UserRoleEnum;
import com.mgg.common.util.ResultVOUtil;
import com.mgg.common.vo.UserVO;
import com.mgg.message.service.notify.NotifyService;
import com.mgg.message.service.sms.SmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController	
public class MessageController {
	
	Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private SmsService smsService;
	
	@Autowired
	private NotifyService notifyService;
	
	/**
	 * 下单成功
	 * @return
	 * @throws Exception
	 */
	@GetMapping(path="/ordernotice")
	public Object sendOrderNotice(@RequestParam("user_id") String userId,@RequestParam("order_id") String orderId) throws Exception {
		UserVO user = smsService.getUserById(userId);
		Map<String,String> paramMap = new HashMap<>();
		paramMap.put("num", orderId.replace("MGG", ""));
		smsService.send(SmsTypeEnum.ORDERNOTICE, user.getPhone(), paramMap);
		notifyService.send(MessageNotifyEnum.ORDERNOTICE, userId, paramMap);
		return ResultVOUtil.success();
	}
	
	/**
	 * 店主商品销售新订单
	 * @return
	 * @throws Exception
	 */
	@GetMapping(path="/paynotice")
	public Object sendPayNotice(@RequestParam("user_id") String userId,
			@RequestParam("super_id") String superId,
			@RequestParam("amount") String amount) throws Exception {
    	
		UserVO superUser = smsService.getUserById(superId);
		String level;
		String roleCode = superUser.getRoleCode();
		if (UserRoleEnum.TUTOR.getCode().equals(roleCode)) {
			level = UserRoleEnum.MANAGER.getName();
		} else if (UserRoleEnum.MANAGER.getCode().equals(roleCode)) {
			level = UserRoleEnum.DIRECTOR.getName();
		} else {
			return ResultVOUtil.success();
		}
		
		UserVO user = smsService.getUserById(userId);
		String name = superUser.getRealName();
		String member = user.getUsername();
		String mobile = superUser.getPhone();
		
		Map<String,String> paramMap = new HashMap<>();
		paramMap.put("name", name);
		paramMap.put("member", member);
		paramMap.put("price", amount);
		paramMap.put("level", level);
		smsService.send(SmsTypeEnum.PAYNOTICE, mobile, paramMap);
		notifyService.send(MessageNotifyEnum.PAYNOTICE, userId, paramMap);
		
		return ResultVOUtil.success();
	}
	
	/**
	 * 推荐店主成功
	 * @return
	 * @throws Exception
	 */
	@GetMapping(path="/newshopkeeper")
	public Object sendNewShopkeeperNotice(@RequestParam("user_id") String userId, @RequestParam String mobile, @RequestParam String name, @RequestParam String phone) throws Exception {
		Map<String,String> paramMap = new HashMap<>();
		paramMap.put("name", name);
		paramMap.put("phone", phone);
		smsService.send(SmsTypeEnum.NEW_SHOPKEEPER, mobile, paramMap);
		notifyService.send(MessageNotifyEnum.NEW_SHOPKEEPER, userId, paramMap);
		return ResultVOUtil.success();
	}
	
	/**
	 * 下级推荐店主成功
	 * @return
	 * @throws Exception
	 */
	@GetMapping(path="/teamshopkeeper")
	public Object sendTeamShopkeeperNotice(@RequestParam("user_id") String userId, @RequestParam String mobile, @RequestParam String shopkeeper, @RequestParam String name, @RequestParam String phone) throws Exception {
		Map<String,String> paramMap = new HashMap<>();
		paramMap.put("shopkeeper", shopkeeper);
		paramMap.put("name", name);
		paramMap.put("phone", phone);
		smsService.send(SmsTypeEnum.TEAM_NEW_SHOPKEEPER, mobile, paramMap);
		notifyService.send(MessageNotifyEnum.TEAM_NEW_SHOPKEEPER, userId, paramMap);
		return ResultVOUtil.success();
	}
	
	/**
	 * 提现申请成功
	 * @return
	 * @throws Exception
	 */
	@GetMapping(path="/withdraw")
	public Object sendWithdrawSubmit(@RequestParam("user_id") String userId) throws Exception {
		notifyService.send(MessageNotifyEnum.WITHDRAW_SUBMIT, userId, null);
		return ResultVOUtil.success();
	}
	
	/**
	 * 提现成功
	 * @return
	 * @throws Exception
	 */
	@GetMapping(path="/withdrawsuc")
	public Object sendWithdrawSuccess(@RequestParam("user_id") String userId, @RequestParam("date") String date) throws Exception {
		UserVO user = smsService.getUserById(userId);
		Map<String,String> paramMap = new HashMap<>();
		paramMap.put("date", date);
		smsService.send(SmsTypeEnum.WITHDRAW_SUCCESS, user.getPhone(), paramMap);
		notifyService.send(MessageNotifyEnum.WITHDRAW_SUCCESS, userId, paramMap);
		return ResultVOUtil.success();
	}
	
	/**
	 * 提现失败
	 * @return
	 * @throws Exception
	 */
	@GetMapping(path="/withdrawfail")
	public Object sendWithdrawFail(@RequestParam("user_id") String userId, @RequestParam("date") String date, @RequestParam("reason") String reason) throws Exception {
		UserVO user = smsService.getUserById(userId);
		Map<String,String> paramMap = new HashMap<>();
		paramMap.put("date", date);
		paramMap.put("reason", reason);
		smsService.send(SmsTypeEnum.WITHDRAW_FAIL, user.getPhone(), paramMap);
		notifyService.send(MessageNotifyEnum.WITHDRAW_FAIL, userId, paramMap);
		return ResultVOUtil.success();
	}
	
	/**
	 * 店主注册成功通知
	 * @return
	 * @throws Exception
	 */
	@GetMapping(path="/shopkeeper")
	public Object sendRegistShopkeeperNotice(@RequestParam("user_id") String userId) throws Exception {
		UserVO user = smsService.getUserById(userId);
		smsService.send(SmsTypeEnum.REGIST_SHOPKEEPER, user.getPhone(), null);
		return ResultVOUtil.success();
	}
	
	/**
	 * 发送店主激活码
	 * @return
	 * @throws Exception
	 */
	@GetMapping(path="/actcode")
	public Object sendShopActCode(@RequestParam("user_id") String userId, @RequestParam("code") String code, @RequestParam("page") String page) throws Exception {
		UserVO user = smsService.getUserById(userId);
		Map<String,String> paramMap = new HashMap<>();
		paramMap.put("code", code);
//		map.put("page", page);
		smsService.send(SmsTypeEnum.SHOP_ACT_CODE, user.getPhone(), paramMap);
		Map<String,String> map = new HashMap<>();
		map.put("code", code);
		map.put("url", "https://mgg.gzqqs.com/web/mgg-activity/#/activeShop");
		notifyService.send(MessageNotifyEnum.SHOP_ACT_CODE, userId, map);
		return ResultVOUtil.success();
	}
	
	@GetMapping(value="/cardcode")
	public Object sendQQSServerCardCode(@RequestParam("user_id") String userId, @RequestParam("code1") String code1, @RequestParam("code2") String code2, @RequestParam("code3") String code3, @RequestParam("code4") String code4) throws Exception {
		UserVO user = smsService.getUserById(userId);
		Map<String,String> paramMap = new HashMap<>();
		paramMap.put("code1", code1);
		paramMap.put("code2", code2);
		paramMap.put("code3", code3);
		paramMap.put("code4", code4);
		smsService.send(SmsTypeEnum.QQS_CARD_CODE, user.getPhone(), paramMap);
		notifyService.send(MessageNotifyEnum.QQS_CARD_CODE, userId, paramMap);
		return ResultVOUtil.success();
	}
}
